package io.renren.modules.subway.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.subway.vo.StationReqVo;
import io.renren.modules.subway.vo.StationRespVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.subway.entity.StationEntity;
import io.renren.modules.subway.service.StationsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 站点
 *
 * @author ${author}
 * @email ${email}
 * @date 2019-04-29 22:08:26
 */
@RestController
@RequestMapping("subway/stations")
public class StationsController {
    @Autowired
    private StationsService stationsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("subway:stations:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = stationsService.listStations(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("subway:stations:info")
    public R info(@PathVariable("id") int id) {
        StationEntity stations = stationsService.getByID(id);

        return R.ok().put("stations", stations);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("subway:stations:save")
    public R save(@RequestBody StationReqVo req) {
        stationsService.createStation(req);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("subway:stations:update")
    public R update(@RequestBody StationEntity stations) {
        ValidatorUtils.validateEntity(stations);
        stations.setLines(null);
        stationsService.updateById(stations);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("subway:stations:delete")
    public R delete(@RequestBody Long[] statIds) {
        stationsService.removeByIds(Arrays.asList(statIds));
        // TODO: delete station_line
        return R.ok();
    }

}
